package com.trontria.simpledatamodel.demo;

import com.trontria.simpledatamodel.Field;
import com.trontria.simpledatamodel.FieldInt;
import com.trontria.simpledatamodel.FieldString;
import com.trontria.simpledatamodel.Model;
import com.trontria.simpledatamodel.ModelDataInterface;

/**
 * 
 * @author TrungNQ
 */
public class Poll extends Model {
	// ========================================================================
	// Constructor
	// ========================================================================
	private static Poll instance;
	public static Poll model() {
		if (instance == null)
			instance = new Poll();
		return instance;
	}
	protected Poll() {
	}
	// ========================================================================
	// Model's metadata
	// ========================================================================
	@Override
	public String getModelName() {
		return "model_" + Poll.class.getSimpleName();
	}
	// ========================================================================
	// Fields
	// ========================================================================
	public static class FieldName {
		public static final String POLL_ID = "id";
		public static final String POLL_QUESTION = "question";
	}
	
	private Field[] fields = {
			FieldInt.field(FieldName.POLL_ID),
			FieldString.field(FieldName.POLL_QUESTION)
		};

	@Override
	public Field[] fieldList() {
		return fields;
	}
	
	// ========================================================================
	// Data
	// ========================================================================
	private int id;
	private String question;
	public static Poll data() {
		return new Poll();
	}
	@Override
	public String getRaw(String key) {
		if (key.equals(FieldName.POLL_ID)) {
			return id + "";
		} else if (key.equals(FieldName.POLL_QUESTION)) {
			return question;
		}
		return null;
	}
	@Override
	public void putRaw(String key, String object) {
		if (key.equals(FieldName.POLL_ID)) {
			id = Integer.parseInt(object);
		} else if (key.equals(FieldName.POLL_QUESTION)) {
			question = object;
		}
	}
	public int getId() {
		return Integer.parseInt(getRaw(FieldName.POLL_ID));
	}
	public void setId(int id) {
		putRaw(FieldName.POLL_ID, id + "");
	}
	public String getQuestion() {
		return getRaw(FieldName.POLL_QUESTION);
	}
	public void setQuestion(String question) {
		putRaw(FieldName.POLL_QUESTION, question);
	}
}